package cn.zymall.merchant.service.impl;

import cn.zymall.common.model.Merchant;

import cn.zymall.common.utils.id.IDUtil;
import cn.zymall.common.utils.redis.RedisUtil;
import cn.zymall.common.utils.staticc.DiscernField;
import cn.zymall.common.utils.time.TimeUtil;
import cn.zymall.merchant.mapper.MerchantAccountMapper;
import cn.zymall.merchant.service.MerchantAccountService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName MerchanAccountService
 * @Description TODO
 * @Author chenmei
 * @Date 2020/8/17 19:53
 * @Version 1.0
 **/
@Service
public class MerchantAccountServiceImpl extends ServiceImpl<MerchantAccountMapper, Merchant> implements MerchantAccountService {


	@Autowired
	MerchantServiceImpl merchantService;

	@Autowired
	RedisUtil redisUtil;

	@Autowired
	TimeUtil timeUtil;

	@Override
	public List<Merchant> getAllMerchant(Integer merchantStatusId) {
		return baseMapper.getAllMerchant(merchantStatusId);
	}

	/**
	 * 获取单个店铺对象
	 * @param merchantId
	 * @return
	 */
	@Override
	public Merchant getSingelMerchant(Integer merchantId) {
		Merchant merchant=baseMapper.getSingelMerchant(merchantId);
		return merchant;
	}

	/**
	 * 审核修改店铺信息
	 * @param merchant 店铺对象
	 * @return 大于0修改成功 ,否则修改失败
	 */
	@Override
	public Integer updataMerchantInfo(Merchant merchant) {
		if(merchant.getMerchantStatusId()==3){
			List<String> account=getMerchantAllAccount();
			String str=judgeAccount(account);
			System.out.println("店铺账号："+str);
			merchant.setMerchantAccount(str); //账号
		}
		return  baseMapper.updataMerchantInfo(merchant);
	}

	public String judgeAccount(List<String> account){
		String str=UUID.randomUUID().toString().substring(2,8);
		for (int i=0;i>account.size();i++){
			if(str.equals(account.get(i))){
				judgeAccount(account);
                return "";
			}
		}
		return str;
	}

	/**
	 * 获取所有非审核失败的店铺账号
	 * @return 所有非审核失败的店铺账号
	 */
	@Override
	public List<String> getMerchantAllAccount() {
		return baseMapper.getMerchantAllAccount();
	}

	/**
	 * 修改店铺状态
	 * @param merchantStatus
	 * @return 大于0表示成功,返之失败
	 */
	@Override
	public Integer updataMerchantStatus(Integer merchantId,Integer merchantStatus) {
		return baseMapper.updataMerchantStatus(merchantId,merchantStatus);
	}

	/**
	 *增加店铺(商家入驻)
	 * @Author: chenmei
	 * @param merchant  店铺实体类
	 * @Date: 2020/8/22 20:55
	 * @return: >0成功,返之失败
	 **/
	@Override
	public Integer addMerchant(Merchant merchant) {
		System.out.println(merchant.getLicenseImg()+"---------------->>>>>>>>>>>>>>>>>>");
		try{
			if(merchant.getMerchantTypeId()==2){//企业店铺
				 addLicenseInfo(merchant);//营业执照
			}
			addReverseCardInfo(merchant);//身份证正面
			addSiteCardInfo(merchant);//身份证反面

		}catch (RuntimeException | ParseException i){
			i.getMessage();
			System.out.println("获取信息报错");
		}finally {
			return baseMapper.addMerchant(merchant);
		}

	}

	/**
	 * 增加身份证正面信息
	 * @Author: chenmei
	 * @param merchant
	 * @Date: 2020/8/17 17:42
	 * @return:
	 **/
	public void addReverseCardInfo(Merchant merchant){
		String reverseCardInfo= DiscernField.card("idcard",merchant.getReverseCardImg());//身份证正面
		System.out.println(reverseCardInfo+"-----------------------------------------身份证面");
		JSONObject jo =  JSONObject.parseObject(reverseCardInfo);
		Object data=jo.get("data");
		Object code=jo.get("code");  //返回状态码
		if("0".equals(code)){
		JSONObject jso=JSONObject.parseObject(data+"");
		merchant.setShopkeeperCardId(jso.get("id_number")+"");//身份证证号
		merchant.setShopkeeperName(jso.get("name")+"");//身份证人名
		merchant.setCardCertificate("证件期有效");
		}
	}

	/**
	 * 增加身份证反面信息
	 * @Author: chenmei
	 * @param merchant
	 * @Date: 2020/8/23 20:29
	 * @return:
	 **/
	public void addSiteCardInfo(Merchant merchant) throws ParseException {
		String siteCardInfo= DiscernField.card("idcard",merchant.getSiteCardImg());//身份证反面
		System.out.println(siteCardInfo+"-----------------------------------------身份证反面");
		JSONObject jo =  JSONObject.parseObject(siteCardInfo);
		Object data=jo.get("data");//
		JSONObject jso=JSONObject.parseObject(data+"");
		String scope=jso.get("validity")+"";
		String[] scopeTime=scope.split("-");
		DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=sdf.parse(scopeTime[1]);
		merchant.setCardEndTime(date);//身份证证号
		System.out.println(data+"-----------------------身份证反面");
	}

	/**
	 * 添加营业执照信息
	 * @Author: chenmei
	 * @param merchant
	 * @Date: 2020/8/23 20:36
	 * @return:
	 **/
	public void addLicenseInfo(Merchant merchant){
		String siteCardInfo= DiscernField.card("business_license",merchant.getLicenseImg());//身份证正面
		JSONObject jo =  JSONObject.parseObject(siteCardInfo);
		Object data=jo.get("data");
		System.out.println(data+"-----------------------营业执照");
	}


	/**
	 * 商家注册 校验手机号,发送短信验证码给用户
	 * @param number 商家手机号
	 * @return
	 */
	public String shortNote(String number){
		String  code=IDUtil.generate8BitUUID().substring(1,7);
		redisUtil.set("shortNote"+number,code,60);
		return code;
	}


	/**
	 * 获取用短信验证码
	 * @param number
	 * @return
	 */
	public Object getShortNote(String number){
		Object code=redisUtil.get("shortNote"+number);
		return code;
	}

}
